package org.flower.isme.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flower.isme.common.exception.BizException;
import org.flower.isme.common.response.BizResponseCode;
import org.flower.isme.flower.domain.dto.PlantEnvironmentDto;
import org.flower.isme.flower.domain.entity.PlantEnvironment;
import org.flower.isme.flower.domain.entity.Plants;
import org.flower.isme.flower.domain.request.PlantEnvironmentRequest;
import org.flower.isme.flower.service.PlantEnvironmentService;
import org.flower.isme.flower.mapper.PlantEnvironmentMapper;
import org.flower.isme.flower.service.PlantsService;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class PlantEnvironmentServiceImpl extends ServiceImpl<PlantEnvironmentMapper, PlantEnvironment>
    implements PlantEnvironmentService{

    private final PlantsService plantsService;

    /**
     * 设置植物生长环境
     *
     * @param plantEnvironment 生长环境信息
     */
    @Override
    public void savePlantEnvironment(PlantEnvironmentRequest plantEnvironment) {
        log.info("当前植物id: {}", plantEnvironment.getPlantId());
        LambdaQueryWrapper<Plants> plantsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        plantsLambdaQueryWrapper.eq(Plants::getId, plantEnvironment.getPlantId());
        boolean exists = plantsService.exists(plantsLambdaQueryWrapper);
        if (!exists) {
            throw new BizException(BizResponseCode.NOT_FOUND, "该植物不存在");
        }

        boolean envirExists = lambdaQuery().eq(PlantEnvironment::getPlantId, plantEnvironment.getPlantId()).exists();
        PlantEnvironment convert = plantEnvironment.convert(PlantEnvironment.class);
        if (!envirExists) {
            log.info("不存在，添加新环境信息");
            baseMapper.insert(convert);
            return;
        }
        log.info("存在，更新新环境信息");
        baseMapper.updateById(convert);
    }

    /**
     * 通过植物id获取生长环境信息
     *
     * @param plantId 植物id
     * @return PlantEnvironmentDto
     */
    @Override
    public PlantEnvironmentDto getPlantEnvironmentDtoById(Long plantId) {
        PlantEnvironment entity = lambdaQuery()
                .eq(PlantEnvironment::getPlantId, plantId)
                .one(); // Use `one()` to get the single entity or null
        return entity != null ? entity.convert(PlantEnvironmentDto.class) : new PlantEnvironmentDto(); // Return an empty DTO if not found
    }

    /**
     * 通过植物id更新花卉详情
     *
     * @param request 更新信息
     */
    @Override
    public void updatePlantEnvironmentByPlantId(PlantEnvironmentRequest request) {
        // 创建更新包装器
        LambdaUpdateWrapper<PlantEnvironment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PlantEnvironment::getPlantId, request.getPlantId());

        // 添加要更新的字段
        if (request.getOptimalHumidityMin() != null) {
            updateWrapper.set(PlantEnvironment::getOptimalHumidityMin, request.getOptimalHumidityMin());
        }
        if (request.getOptimalHumidityMax() != null) {
            updateWrapper.set(PlantEnvironment::getOptimalHumidityMax, request.getOptimalHumidityMax());
        }
        if (request.getOptimalTemperatureMin() != null) {
            updateWrapper.set(PlantEnvironment::getOptimalTemperatureMin, request.getOptimalTemperatureMin());
        }
        if (request.getOptimalTemperatureMax() != null) {
            updateWrapper.set(PlantEnvironment::getOptimalTemperatureMax, request.getOptimalTemperatureMax());
        }
        if (request.getBlossom() != null) {
            updateWrapper.set(PlantEnvironment::getBlossom, request.getBlossom());
        }
        if (request.getOptimalLightIntensity() != null) {
            updateWrapper.set(PlantEnvironment::getOptimalLightIntensity, request.getOptimalLightIntensity());
        }
        if (request.getFertilizationFrequency() != null) {
            updateWrapper.set(PlantEnvironment::getFertilizationFrequency, request.getFertilizationFrequency());
        }

        if (request.getOptimalSoilMoisture() != null) {
            updateWrapper.set(PlantEnvironment::getOptimalSoilMoisture, request.getOptimalSoilMoisture());
        }

        // 执行更新
        baseMapper.update(null, updateWrapper);
    }


}




